package com.EE7th.service.impl;

import com.EE7th.Result.PageResult;
import com.EE7th.dao.TeacherDao;
import com.EE7th.domain.Teacher;
import com.EE7th.dto.PageQueryDTO;
import com.EE7th.dto.TeacherDTO;
import com.EE7th.service.TeacherService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherDao teacherDao;

    @Override
    public void insertTeacher(Teacher teacher) {
        teacherDao.insertTeacher(teacher);
    }

    @Override
    public void deleteTeacher(int id) {
        teacherDao.deleteTeacher(id);
    }

    @Override
    public void linkTeacherClass(int teacherId, int classId) {
        teacherDao.linkTeacherClass(teacherId, classId);
    }

    @Override
    public void unlinkTeacherClass(int teacherId, int classId) {
        teacherDao.unlinkTeacherClass(teacherId, classId);
    }

    @Override
    public List<TeacherDTO> getTeacherSelection() {
        return teacherDao.getTeacherSelection().stream()
                .map(teacher -> {
                    TeacherDTO dto = new TeacherDTO();
                    dto.setTeacherId(teacher.getTeacherId());
                    dto.setName(teacher.getName());
                    return dto;
                })
                .collect(Collectors.toList());
    }

    @Override
    public PageResult pageQuery(PageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        List<Teacher> teachers = teacherDao.pageQuery();
        PageInfo<Teacher> pageInfo = new PageInfo<>(teachers);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }
}